

import COS from 'cos-js-sdk-v5';
import utils from '@/js/until.js';
const getLoading = utils.globleLoading();


var nowDate= new Date();
var dateYear = nowDate.getFullYear();
var dateMother = nowDate.getMonth()+1;
var dateDay = nowDate.getDate();

const imgUrl ='https://image.gama.city'

// SECRETID 和 SECRETKEY 请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
var cos = new COS({
    SecretId: 'IKIDKepqjWTRHEKH4RimhWFrMjyIQPGzWwDH',
    SecretKey: '2veAWmy8HI9rZBdzFNMG5mMqHh07Lj7T',
});

function addPhoto(timeStamp,callBack) {
    getLoading.show();

    var fileText = `${dateYear}${dateMother}${dateDay}`;
    var files = document.getElementById(`photoupload${timeStamp}`).files;
    if (!files.length) {
        return alert("Please choose a file to upload first.");
    }
    let num= Math.floor(Math.random() * 90000000) + 10000000;

    var file = files[0];
    var fileName = `/banner/${fileText}${num}.png`;
    var albumPhotosKey = encodeURIComponent('action') + fileText;

    // Use S3 ManagedUpload class as it supports multipart uploads
    cos.uploadFile({
        Bucket: 'gama-1321572567', /* 填入您自己的存储桶，必须字段 */
        Region: 'eu-frankfurt',  /* 存储桶所在地域，例如ap-beijing，必须字段 */
        Key: fileName,  /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
        Body: file, /* 必须，上传文件对象，可以是input[type="file"]标签选择本地文件后得到的file对象 */
        SliceSize: 1024 * 1024 * 5,     /* 触发分块上传的阈值，超过5MB使用分块上传，非必须 */
        // onTaskReady: function(taskId) {                   /* 非必须 */
        //     console.log(taskId);
        // },
        // onProgress: function (progressData) {           /* 非必须 */
        //     console.log(JSON.stringify(progressData));
        // },
        // onFileFinish: function (err, data, options) {   /* 非必须 */
        //     console.log(options.Key + '上传' + (err ? '失败' : '完成'));
        // },
        // 支持自定义headers 非必须
        Headers: {
            'x-cos-meta-test': 123
        },
    }, function(err, data) {
        // console.log(err || data);
        if (getLoading.loadingConfig.countLoading > 0) {
            getLoading.loadingConfig.countLoading -= 1;
        }
        getLoading.hide();
        if(data) {
            callBack&&callBack(`${imgUrl}${fileName}`)
        }   else {
            callBack&&callBack(err)

        }
    });

}



function awsUploadImg(){
    return new Promise((resolve, reject)=>{
        var timeStamp =Math.round(new Date().getTime()/1000).toString();
        var inputCentont=' <input  accept="image/*" type="file" id="'+`photoupload${timeStamp}`+'">'
        var inputBox= document.createElement('div');
        inputBox.id=`aws_Albums${timeStamp}`
        inputBox.style.width=0;
        inputBox.style.height=0;
        inputBox.style.position='fixed';
        inputBox.style.top='10000000px';
        inputBox.style.left='10000000px';
        inputBox.style.overflow='hidden';
        inputBox.innerHTML = inputCentont;
        document.querySelector('body').appendChild(inputBox);
        document.getElementById(`photoupload${timeStamp}`).addEventListener('change',()=>{
            addPhoto(timeStamp, (url)=>{
                resolve(url);

            })
        })
        document.getElementById(`photoupload${timeStamp}`).click();
    })

}

export default {
    awsUploadImg
}
